package gui.drawers;

import gui.GameConstants;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import dal.MapStruct;
import dal.object.Ball;
import dal.object.Card;
import dal.object.MapObject;

@SuppressWarnings("serial")
public class HoverDrawer extends Drawer {
	private int x, y;
	private int j = -1, i = -1;
	private MapObject hover;
	
	public HoverDrawer(MapStruct ms){
		this.setOpaque(false);
		this.setSize(ms.getSize());
	}
	
	/**
	 * @param j, i
	 * 	grid indexes
	 * @param mo
	 * 	the current object that will be drawn as hover
	 * @param drawable
	 */
	public void drawHover(int j, int i, MapObject mo, boolean drawable){
		if(this.i != i || this.j != j){
			if(drawable){
				if(hover != mo){
					hover = mo;
					
					if(hover instanceof Ball){
						img = mo.getImage();
						img = new BufferedImage(img.getWidth() + Ball.getDx(), img.getHeight() + Ball.getDy(), BufferedImage.TRANSLUCENT);
						Graphics2D grap = (Graphics2D) img.getGraphics();
						grap.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, 0.65F));
						grap.drawImage(mo.getImage(), Ball.getDx(), Ball.getDy(), null);
					} else {
						if(mo instanceof Card){
							((Card) mo).setVisible(true);
						}
						
						img = mo.getImage();
						img = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TRANSLUCENT);
						Graphics2D grap = (Graphics2D) img.getGraphics();
						grap.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, 0.65F));
						grap.drawImage(mo.getImage(), 0, 0, null);
					}
				}
				
				this.x = GameConstants.getXbyIndex(j, i);
				this.y = GameConstants.getYbyIndex(i);
			} else {
				hover = null;
				img = null;
			}
			
			this.i = i;
			this.j = j;
			
			repaint();
		}
	}
	
	public void paintComponent(Graphics g){
		g.drawImage(img, x, y, null);
	}
	
	/**
	 * deletes everything and repaints all again
	 */
	public void repaintAll() {
		repaint();
	}
}
